$(function(){
    $('form').hide();
    
    var ajaxFill = new Z.AjaxFill({
        url: Z.url('/backend/message/records',true),
        templates: {
            list: '<tr>\
                        <td><input type="checkbox" name="id[]" value="${id}"/></td>\
                        <td>${name}</td>\
                        <td>${telephone}</td>\
                        <td>${email}</td>\
                        <td>${address}</td>\
                        <td>${content}</td>\
                        <td>${ip}</td>\
                        <td>${created_at}</td>\
                        <td class="action">\
                            <a href="javascript:void(0);" class="edit">编辑</a>\
                            <a href="javascript:void(0);" class="delete">删除</a>\
                            <a href="javascript:void(0);" class="hide">隐藏</a></div>\
                        </td>\
                    </tr>',
            none: '<tr><td colspan="9" style="text-align:center;">记录为空</td></tr>',
            loading: '<tr><td colspan="9" style="text-align:center;"><img src="../assets/img/backend/loading.gif"/></td></tr>'
        },
		data : {
			pageSize : 20
		},
        containers: {
            list: $('table.records tbody'),
            pageLinks: $('table.records tfoot td:eq(1)')
        }
    });
    
    ajaxFill.request();
    
    $('a.delete').live('click',function(){
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.confirm({
            content: 'r u sure?',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/message/delete/'+id,true),
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
        return false;
    });
    
    $('a.hide').live('click',function(){
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.ajax({
            url: Z.url('/backend/message/hide/'+id,true),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
    
    $('a.select-all').click(function(){
        $('table.records tbody td input:checkbox:not(:checked)').trigger('click');
    });
    
    $('a.select-reverse').click(function(){
        $('table.records tbody td input:checkbox').trigger('click');
    });
    
    $('a.delete-batch').click(function(){
        var ids = [];
        $('table.records tbody td input:checkbox:checked').each(function(){
            ids.push(Z.parseInt($(this).val()));
        });
        Z.confirm({
            content: 'r u sure?',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/message/delete_batch',true),
                    data: {
                        ids:ids
                    },
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
    });
    
    $('a.add').click(function(){
        $('table.records').hide();
        $('form').show();
        $('form:eq(0)').attr('action',$('form:eq(0)').attr('action').replace(/\/update$/,'/insert'))
        Z.cleanForm($('form:eq(0)'));
    });
    
    $('a.edit').live('click',function(){
        $('table.records').hide();
        $('form').show();
        $('form:eq(0)').attr('action',$('form:eq(0)').attr('action').replace(/\/insert$/,'/update'))
        var id = Z.parseInt($(this).parent().siblings().first().find('input:checkbox').val());
        Z.ajax({
            url: Z.url('/backend/message/find/'+id,true),
            success: function(data){
                Z.fillForm($('form:eq(0)'),data);
            }
        });
        return false;
    });
    
    $('a.back').click(function(){
        $('table.records').show();
        $('form').hide();
    });
    
    $('form').submit(function(){
        Z.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });
});